package client

import (
	"context"
	"flag"

	"example.com/dcache/logger"
	pb "example.com/dcache/rpcserver/dcacher"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

const (
	defaultName = "World"
)

var (
	addr = flag.String("addr", "127.0.0.1:50051", "the server to connect")
	name = flag.String("name", defaultName, "the name")
)

//var grpcClient pb.DcacherClient
/*
func main() {
	flag.Parse()

	conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}

	defer func(conn *grpc.ClientConn) {
		conn.Close()
	}(conn)

	//listValue()
}*/

type RpcClient struct {
	addr       string
	conn       *grpc.ClientConn
	grpcClient pb.DcacherClient
}

func NewRpcClient(addr string) *RpcClient {
	rc := RpcClient{}
	rc.addr = addr
	var err error

	rc.conn, err = grpc.Dial(rc.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		logger.Error("<rpcclient>can not connect rpcserver addr: %s", err)
	}

	rc.grpcClient = pb.NewDcacherClient(rc.conn)

	return &rc
}

func (rc *RpcClient) SetGroup(groupName string) int64 {
	// 创建发送结构体, 组名
	req := pb.SetGroupRequest{
		Group: groupName,
	}
	// 调用我们的服务(SetGroup方法)
	reply, err := rc.grpcClient.SetGroup(context.Background(), &req)
	if err != nil {
		logger.Error("<rpcclient> call SetGroup err: %s", err)
	}

	//返回状态与其他信息
	status := reply.GetStatus()
	reply.GetInfo()

	logger.Debug("<rpcclient> call SetGroup remotely on: %s", rc.addr)
	return status
}

func (rc *RpcClient) DeleteGroup(groupName string) int64 {
	// 创建发送结构体, 组名
	req := pb.DeleteGroupRequest{
		Group: groupName,
	}
	// 调用我们的服务(SetGroup方法)
	reply, err := rc.grpcClient.DeleteGroup(context.Background(), &req)
	if err != nil {
		logger.Error("<rpcclient> call DeleteGroup err: %s", err)
	}

	//返回状态与其他信息
	status := reply.GetStatus()
	reply.GetInfo()

	logger.Debug("<rpcclient> call DeleteGroup remotely on: %s", rc.addr)
	return status
}

func (rc *RpcClient) Set(groupName string, key string, value string) int64 {
	// 创建发送结构体
	req := pb.SetRequest{
		Group: groupName,
		Key:   key,
		Value: value,
	}
	// 调用我们的服务
	reply, err := rc.grpcClient.Set(context.Background(), &req)
	if err != nil {
		logger.Error("<rpcclient> call Set err: %s", err)
	}

	//返回状态与其他信息
	status := reply.GetStatus()
	reply.GetInfo()

	logger.Debug("<rpcclient> call Set remotely on: %s", rc.addr)
	return status
}

func (rc *RpcClient) Get(groupName string, key string) (int64, string) {
	// 创建发送结构体, 组名
	req := pb.GetRequest{
		Group: groupName,
		Key:   key,
	}
	// 调用我们的服务(SetGroup方法)
	reply, err := rc.grpcClient.Get(context.Background(), &req)
	if err != nil {
		logger.Error("<rpcclient> call Get err: %s", err)
	}

	//返回状态与其他信息
	status := reply.GetStatus()
	value := reply.GetValue()
	reply.GetInfo()

	logger.Debug("<rpcclient> call Get remotely on: %s", rc.addr)
	return status, value
}

func (rc *RpcClient) Delete(groupName string, key string) int64 {
	// 创建发送结构体, 组名
	req := pb.DeleteRequest{
		Group: groupName,
		Key:   key,
	}
	// 调用我们的服务(Delete方法)
	reply, err := rc.grpcClient.Delete(context.Background(), &req)
	if err != nil {
		logger.Error("<rpcclient> call Delete err: %s", err)
	}

	//返回状态与其他信息
	status := reply.GetStatus()
	reply.GetInfo()

	logger.Debug("<rpcclient> call Delete remotely on: %s", rc.addr)
	return status
}
